package com.sqx.modules.trendsComment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.message.entity.MessageInfo;
import com.sqx.modules.message.service.MessageService;
import com.sqx.modules.trends.dao.TrendsDao;
import com.sqx.modules.trends.entity.Trends;
import com.sqx.modules.trends.service.ITrendsService;
import com.sqx.modules.trendsComment.dao.TrendsCommentDao;
import com.sqx.modules.trendsComment.entity.TrendsComment;
import com.sqx.modules.trendsComment.service.ITrendsCommentService;
import com.sqx.modules.trendsComment.vo.TrendsCommentVo;
import com.sqx.modules.utils.BaiDuUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 动态评论 服务实现类
 *
 * @author wuChang
 * @since 2023-09-26
 */
@Service
public class TrendsCommentServiceImpl extends ServiceImpl<TrendsCommentDao, TrendsComment> implements ITrendsCommentService {
    @Autowired
    private ITrendsService iTrendsService;
    @Autowired
    private UserService userService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private CommonInfoService commonInfoService;
    @Autowired
    private TrendsDao trendsDao;

    @Override
    public Result saveTrendsComment(TrendsComment trendsComment) {
        String value = commonInfoService.findOne(294).getValue();
        if ("是".equals(value)) {
            if (StringUtils.isNotBlank(trendsComment.getCommentContent())) {
                if (BaiDuUtils.auditBaiDuText(trendsComment.getCommentContent())) {
                    return Result.error("您发布的内容包含敏感词汇");
                }
            }
        }
        Trends trends = iTrendsService.getById(trendsComment.getTrendsId());
        if (trends == null || trends.getStatus() != 1) {
            return Result.error("动态已删除或已屏蔽");
        }
        if (trendsComment.getParentCommentId() != null) {
            TrendsComment comment = baseMapper.selectById(trendsComment.getParentCommentId());
            if (comment == null) {
                return Result.error("评论不存在");
            }
            trendsComment.setByUserId(comment.getUserId());
        }
        if (trendsComment.getCommentId() != null) {
            return Result.upStatus(baseMapper.updateById(trendsComment));
        } else {
            UserEntity userEntity = userService.getById(trendsComment.getUserId());
            MessageInfo messageInfo = new MessageInfo();
            messageInfo.setContent("用户" + userEntity.getUserName() + "评论了你的动态《" + trends.getTrendsContent() + "》");
            messageInfo.setTitle("评论通知");
            messageInfo.setState(String.valueOf(6));
            messageInfo.setUserId(trends.getUserId().toString());
            messageInfo.setCreateAt(DateUtils.format(new Date()));
            messageInfo.setIsSee("0");
            messageService.saveBody(messageInfo);
            trendsComment.setIsLook(0);
            trendsComment.setCreateTime(LocalDateTime.now());
            return Result.upStatus(baseMapper.insert(trendsComment));
        }
    }

    @Override
    public IPage<TrendsCommentVo> getCommentList(Integer page, Integer limit, Long byUserId, TrendsComment trendsComment) {

        Page<TrendsCommentVo> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        return baseMapper.getCommentList(pages, byUserId, trendsComment);
    }

    @Override
    public IPage<TrendsCommentVo> getAppCommentList(Integer page, Integer limit, Long byUserId, TrendsComment trendsComment) {
        Page<TrendsCommentVo> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        IPage<TrendsCommentVo> appCommentList = baseMapper.getAppCommentList(pages, byUserId, trendsComment);
        for (TrendsCommentVo record : appCommentList.getRecords()) {
            record.setCommentId(record.getCommentId());
            List<TrendsCommentVo> trendsList = baseMapper.selectListByCommentId(record);
            if (trendsList != null) {
                record.setCommentList(trendsList);
            }
        }
        return appCommentList;
    }

    @Override
    public IPage<TrendsCommentVo> getUnreadCommentList(Integer page, Integer limit, Long userId) {
        Page<TrendsCommentVo> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        IPage<TrendsCommentVo> unreadCommentList = baseMapper.getUnreadCommentList(pages, userId);
        TrendsComment trendsComment = new TrendsComment();
        trendsComment.setIsLook(1);
        baseMapper.update(trendsComment, new QueryWrapper<TrendsComment>().eq("is_look", 0).eq("by_user_id", userId));

        return unreadCommentList;
    }

    @Override
    public Result replyTrendsComment(TrendsComment trendsComment) {
        UserEntity user = userService.getById(trendsComment.getUserId());
        if (user == null) {
            return Result.error("用户不存在，请重新登录");
        }
        String value = commonInfoService.findOne(332).getValue();
        if ("2".equals(value)) {
            if (StringUtils.isNotBlank(trendsComment.getCommentContent())) {
                if (BaiDuUtils.auditBaiDuText(trendsComment.getCommentContent())) {
                    return Result.error("您回复的内容包含敏感词汇");
                }
            }
        }
        if (trendsComment.getTrendsId() != null) {
            Trends trends = iTrendsService.getById(trendsComment.getTrendsId());
            if (trends == null) {
                return Result.error("动态不存在，请刷新后重试");
            }
            if (trendsComment.getByUserId() == null) {
                return Result.error("回复对象不能为空");
            }
            if (!trendsComment.getUserId().equals(trends.getUserId())) {
                return Result.error("您无权操作");
            }
            TrendsComment trendsComment1 = new TrendsComment();
            trendsComment1.setParentCommentId(trendsComment.getCommentId());
            trendsComment1.setTrendsId(trendsComment.getTrendsId());
            trendsComment1.setUserId(trendsComment.getUserId());
            trendsComment1.setCommentContent(trendsComment.getCommentContent());
            trendsComment1.setByUserId(trendsComment.getByUserId());
            trendsComment1.setCreateTime(LocalDateTime.now());
            trendsComment1.setIsLook(0);
            int result = baseMapper.insert(trendsComment1);
            if (result > 0) {
                UserEntity userEntity = userService.getById(trendsComment.getByUserId());
                if (userEntity != null) {
                    MessageInfo messageInfo = new MessageInfo();
                    messageInfo.setContent("用户：" + user.getUserName() + "，给您回复了动态《" + trendsComment.getCommentContent() + "》");
                    messageInfo.setTitle("动态回复");
                    messageInfo.setState(String.valueOf(6));
                    messageInfo.setUserId(trendsComment.getByUserId().toString());
                    messageInfo.setCreateAt(DateUtils.format(new Date()));
                    messageInfo.setIsSee("0");
                    messageService.saveBody(messageInfo);
                }
            } else {
                return Result.error("回复失败");
            }
            return Result.success();
        }
        return Result.error("动态不存在，请刷新后重试");
    }

    @Override
    public Result deleteCommentById(Long commentId, Long userId) {
        TrendsComment trendsComment = baseMapper.selectById(commentId);
        if (trendsComment == null) {
            return Result.error("评论内容不存在");
        }
        UserEntity userEntity = userService.getById(userId);
        if (userEntity == null) {
            return Result.error("用户信息已失效,请重新登录");
        }
        Trends trends = trendsDao.selectById(trendsComment.getTrendsId());
        if (!userId.equals(trends.getUserId())) {
            return Result.error("非发布人不可操作");
        }
        trendsComment.setIsDelete(1);
        int result = baseMapper.updateById(trendsComment);
        if (result <= 0) {
            return Result.error("删除失败");
        } else {
            List<TrendsComment> trendsCommentList = baseMapper.selectList(new QueryWrapper<TrendsComment>().eq("parent_comment_id", commentId));
            if (trendsCommentList != null) {
                for (TrendsComment trendsComment1 : trendsCommentList) {
                    trendsComment1.setIsDelete(1);
                    baseMapper.updateById(trendsComment1);
                }
            }
        }
        return Result.success();
    }

    @Override
    public IPage<TrendsCommentVo> getAdminCommentList(Integer page, Integer limit, TrendsComment trendsComment) {
        Page<TrendsCommentVo> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        IPage<TrendsCommentVo> trendsCommentVoIPage = baseMapper.getAdminCommentList(pages, trendsComment);
        return trendsCommentVoIPage;
    }


}
